package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    /**查询今日运营数据
     *
     * @return
     */
    @Override
    @Transactional
    public BusinessDataVO getToday(LocalDateTime beginTime, LocalDateTime endTime) {
        /*LocalDate now = LocalDate.now();
        LocalDateTime beginTime = LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(now,LocalTime.MAX);*/
        /*//获得当天的开始时间
        LocalDateTime beginTime = LocalDateTime.now().with(LocalTime.MIN);
        //获得当天的结束时间
        LocalDateTime endTime = LocalDateTime.now().with(LocalTime.MAX);*/
        /*统计今日营业额数据*/
        Map map = new HashMap<>();
        map.put("begin",beginTime);
        map.put("end",endTime);
        map.put("status", Orders.COMPLETED);
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;
        //有效订单数
        Integer validOrderCount = orderMapper.countByMap(map);
        /*订单完成率*/
        //总订单数
        Integer orderCount = getOrderCount(beginTime, endTime, null);

        Double orderCompletionRate =0.0;
        if (orderCount != 0){
            orderCompletionRate =  validOrderCount.doubleValue() / orderCount;
        }
        /*平均客单价*/
        double unitPrice = 0.0;
        if (validOrderCount!=0){
            unitPrice = turnover / validOrderCount;
        }

        /*新增用户数*/
        Map mapuser = new HashMap<>();
        mapuser.put("begin",beginTime);
        mapuser.put("end",endTime);
        Integer newUser = userMapper.countByMap(mapuser);



        return BusinessDataVO.builder()
                .turnover(turnover)
                .unitPrice(unitPrice)
                .newUsers(newUser)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }

    /**查询订单管理数据
     *
     * @return
     */
    @Override
    public OrderOverViewVO getOrder() {
        //获得全部订单
        Integer allOrders = orderMapper.getOrderCountByStatus(null);
        //已取消订单
        Integer cancelledOrders = orderMapper.getOrderCountByStatus(Orders.CANCELLED);
        //已完成订单
        Integer completedOrders = orderMapper.getOrderCountByStatus(Orders.COMPLETED);
        //待派送订单
        Integer deliveredOrders = orderMapper.getOrderCountByStatus(Orders.CONFIRMED);
        //待接单订单
        Integer waitingOrders = orderMapper.getOrderCountByStatus(Orders.TO_BE_CONFIRMED);

        return OrderOverViewVO.builder()
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .allOrders(allOrders)
                .build();
    }

    /**查询菜品总览
     *
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        //已起售
        Integer sold = dishMapper.getDishCountByStatus(StatusConstant.ENABLE);
        //已停售
        Integer discontinued = dishMapper.getDishCountByStatus(StatusConstant.DISABLE);

        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        //已起售
        Integer sold = setmealMapper.getSetmealCountByStatus(StatusConstant.ENABLE);
        //已停售
        Integer discontinued = setmealMapper.getSetmealCountByStatus(StatusConstant.DISABLE);

        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 根据条件统计订单数量
     * @param begin
     * @param end
     * @param status
     * @return
     */
    private Integer getOrderCount(LocalDateTime begin,LocalDateTime end,Integer status){
        Map map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        map.put("status",status);

        return orderMapper.countByMap(map);

    }

}
